<template>
  <view class="perception" v-if="listv">
    <view
      class="perception_item"
      v-for="(item, index) in listv.data"
      :key="index"
      @click="toDetail(item)"
    >
      <view class="item_t" v-if="item.user">
        <view class="perception_t_left" @click.stop="toUser(item)">
          <image :src="item.user.avatar_url" mode=""></image>
          <text class="perception_t_text">{{ item.user.nick_name }}</text>
        </view>
        <view
          class="perception_t_right"
          v-show="flag && item.user.is_guanzhu == 0"
          @click.stop="guanzhu(1, item)"
        >
          <u-icon
            name="plus"
            color="#55B877"
            style="margin-right: 10rpx"
          ></u-icon>
          关注
        </view>
        <view
          class="perception_t_right1"
          v-show="flag && item.user.is_guanzhu != 0"
          @click.stop="guanzhu(2, item)"
        >
          已关注
        </view>
        <view v-show="!flag" class="item_left_clickView" @click.stop="change(item)">
          <image
            v-if="!item.clickable"
            class="item_left_click"
            src="../../../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_clickable"
            src="../../../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
      </view>
      <view class="item_c">
        <view class="perception_c_left">
          {{ item.content || item.msg }}
        </view>
        <view class="perception_c_right" v-if="item.file && item.file.length">
          <image :src="item.file[0].url_img" mode=""></image>
        </view>
      </view>
      <view class="item_b" v-if="!item.msg">
        <view class="zanBtn" @click="dianZan()">
          <image
            class="zanImg"
            src="@/static/weichuang/icon_fld_dianzan.png"
          ></image>
          <view class="zanFont" style="margin-right: 43rpx">{{
            item.like_count
          }}</view>
        </view>
        <view class="zanBtn" @click="pinglun()">
          <image
            class="zanImg"
            src="@/static/weichuang/icon_fld_pinglun.png"
          ></image>
          <view class="zanFont">{{ item.comment_count }}</view>
        </view>
      </view>
    </view>

    <view class="bottom" v-show="!flag">
      <view class="bottom_left">
        <view @click="changeClick">
          <image
            v-if="!clickableAll"
            class="item_left_click"
            src="../../../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_click"
            src="../../../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
        <view class="bottom_left_text">全选</view>
      </view>
      <view class="bottom_right" @click="deleteFn">删除</view>
    </view>

    <u-modal
      :show="show"
      :title="title"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm"
      @close="close"
      @cancel="cancel"
      :showCancelButton="true"
    ></u-modal>

    <view class="empty_view" v-if="listv.data && listv.data.length == 0">
      <image
        class="item_left_clickable"
        src="../../../static/images/my/kong.png"
        mode=""
      ></image>
      <view v-if="from == 'history'">您还没有历史记录~</view>
      <view v-else>当前没有相关数据，请查看其他分类或功能。</view>
    </view>
  </view>
</template>

<script>
import { userGuanzhu } from "@/common/http.js";
export default {
  components: {},
  props: {
    flag: {
      type: Boolean,
      default: true,
    },
    list: {
      type: Object,
      default: () => {},
    },
    from: {
      type: String,
      default: "",
    },
  },
  watch: {
    list: {
      deep: true, // 深度监听每一个属性值, 如果是对象可以深度监听
      immediate: true, // 进入页面后立即自动调用handler函数
      handler(newValue) {
        this.listv = newValue;
        if (this.listv && this.listv.data) {
          for (let i = 0; i < this.listv.data.length; i++) {
            this.listv.data[i].clickable = false;
          }
        }
        // console.log(this.listv, "newList");
      },
    },
  },
  data() {
    return {
      clickableAll: false,
      listv: [],
      show: false,
      title: "确认删除",
      content: "确认删除选中内容？",
    };
  },
  methods: {
    change(item) {
      this.$forceUpdate();
      item.clickable = !item.clickable;
      // 判断是否全选
      let all = true;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == false) {
          all = false;
        }
      }
      if (all) {
        this.clickableAll = true;
      } else {
        this.clickableAll = false;
      }
    },
    changeClick() {
      this.clickableAll = !this.clickableAll;
      if (this.clickableAll) {
        for (let i = 0; i < this.listv.data.length; i++) {
          this.listv.data[i].clickable = true;
        }
      } else {
        for (let i = 0; i < this.listv.data.length; i++) {
          this.listv.data[i].clickable = false;
        }
      }
    },
    pinglun() {},
    dianZan() {},
    confirm() {
      this.show = false;
      let arr = [];
      let num = 0;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == true) {
          arr.push(this.listv.data[i].history_id);
          num++;
        }
      }
      console.log(arr, "数组");
      this.deletFn(arr);
    },
    close() {
      this.show = false;
    },
    cancel() {
      this.show = false;
    },
    // 删除按钮
    deleteFn() {
      let isTrue = true;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == true) {
          isTrue = false;
        }
      }
      if (isTrue == true) {
        uni.$u.toast("您还未选择内容");
      } else {
        this.show = true;
      }
    },
    // 删除接口
    deletFn(arr) {
      this.$http
        .historyDel_his_yitian({
          ids: arr,
        })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show = false;
          setTimeout(() => {
            this.$emit("updateDataYanXue");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    toUser(item) {
      uni.navigateTo({
        url: `/pages/home/home?user_id=${item.user.user_id}`,
      });
    },
    toDetail(item) {
      console.log("itemitemitem", item);
      let type = "fei";
      if (item.type == 1) {
        type = "fei";
      } else if (item.type == 2) {
        type = "ling";
      } else if (item.type == 3) {
        type = "shi";
      } else if (item.type == 4) {
        type = "wen";
      } else if (item.type == 6) {
        type = "youji";
      }
      uni.navigateTo({
        url: `/pages/detail/flyDetail?detailType=${type}&currentId=${item.id}&from=yitian`,
      });
    },
    guanzhu(type, item) {
      let that = this;
      if (type == 2) {
        userGuanzhu({
          user_id: item.user.user_id,
        })
          .then((res) => {
            uni.$u.toast("取消关注成功");
            that.listv.data.forEach((val, index) => {
              if (val.user.user_id == item.user.user_id) {
                that.listv.data[index].user.is_guanzhu = 0;
              }
            });
          })
          .catch((err) => {
            this.$u.toast(err);
          });
      } else {
        userGuanzhu({
          user_id: item.user.user_id,
        })
          .then((res) => {
            uni.$u.toast("关注成功");
            that.listv.data.forEach((val, index) => {
              if (val.user.user_id == item.user.user_id) {
                that.listv.data[index].user.is_guanzhu = 1;
              }
            });
          })
          .catch((err) => {
            this.$u.toast(err);
          });
      }
      console.log("33333", that.listv);
      this.$forceUpdate();
    },
  },
};
</script>

<style scoped lang="scss">
.perception {
  width: 100%;
  border-radius: 16rpx 16rpx 16rpx 16rpx;
  padding: 0 30rpx;
  box-sizing: border-box;

  .perception_item {
    width: 100%;
    padding: 32rpx;
    box-sizing: border-box;
    margin-top: 32rpx;
    background: rgba(39, 39, 39, 0.5);

    .item_t {
      width: 100%;
      display: flex;
      justify-content: space-between;

      .perception_t_left {
        display: flex;
        align-items: center;

        image {
          width: 72rpx;
          height: 72rpx;
          border-radius: 50%;
        }

        .perception_t_text {
          font-size: 28rpx;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          color: #eeeeee;
          margin-left: 24rpx;
        }
      }

      .perception_t_right {
        width: 142rpx;
        height: 56rpx;
        border-radius: 32rpx;
        background: rgba(85, 184, 119, 0.1);
        border: 1rpx solid #55b877;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 26rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #55b877;
      }

      .perception_t_right1 {
        width: 142rpx;
        height: 56rpx;
        border-radius: 32rpx;
        background: rgba(85, 184, 119, 0.1);
        border: 1rpx solid #adadad;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 26rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #adadad;
      }

      .item_left_clickView {
        margin-right: 24rpx;

        .item_left_click {
          width: 40rpx;
          height: 40rpx;
        }

        .item_left_clickable {
          width: 40rpx;
          height: 40rpx;
        }
      }
    }

    .item_c {
      width: 100%;
      margin-top: 32rpx;
      display: flex;

      .perception_c_left {
        width: 506rpx;
        font-size: 30rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #eeeeee;
        word-break: break-word; //换行模式
        overflow: hidden;
        text-overflow: ellipsis; //修剪文字，超过2行显示省略号
        display: -webkit-box;
        -webkit-line-clamp: 2; //此处为上限行数
        -webkit-box-orient: vertical;
      }

      .perception_c_right {
        width: 96rpx;
        height: 96rpx;
        margin-left: 24rpx;

        image {
          width: 100%;
          height: 100%;
        }
      }
    }

    .item_b {
      width: 100%;
      display: flex;
      margin-top: 16rpx;

      .zanBtn {
        display: flex;
        align-items: center;

        .zanImg {
          width: 40rpx;
          height: 40rpx;
        }

        .zanFont {
          margin: 0 10rpx;
          line-height: 40rpx;
          font-size: 24rpx;
          color: #adadad;
        }
      }
    }
  }

  .bottom {
    width: 100%;
    height: 112rpx;
    background: #272727;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 16rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .bottom_left {
      display: flex;
      align-items: center;

      .item_left_click {
        width: 40rpx;
        height: 40rpx;
      }

      .bottom_left_text {
        margin-left: 16rpx;
        color: #eeeeee;
        font-size: 30rpx;
      }
    }

    .bottom_right {
      width: 250rpx;
      height: 80rpx;
      background: #ee6060;
      border-radius: 48rpx;
      text-align: center;
      line-height: 80rpx;
      font-size: 30rpx;
      color: #eeeeee;
    }
  }

  /deep/.u-modal__content__text {
    text-align: center;
  }

  .empty_view {
    margin-top: 300rpx;

    image {
      margin: 0 auto;
      width: 410rpx;
      height: 410rpx;
    }

    view {
      font-size: 30rpx;
      color: #d8d8d8;
      text-align: center;
    }
  }
}
</style>
